<?php
namespace app\jiangnan\controller;
use think\Db;
use think\Controller;
use think\Log;
use app\service\apixcxService;
use app\service\personService;
class Visitor extends Controller
{
	//添加用户操作
	public function visitoradd()
	{
		$user_id=request()->post("user_id");
		$name=request()->post("name");
		$phone=request()->post("phone");
		
		$res=(new personService())->visitoradd($user_id,$name,$phone);
		return $res;
	}
	//业主验证
	public function verifyOwner()
	{
		$user_id=request()->post("user_id");
		if(empty($user_id))
		{
			return json(["errCode"=>"1",'errormsg' => '您不是业主无权访问']);
		}
		//查询是否是业主
		$userinfo=Db::table("personnel")
							->where("user_id",$user_id)
							->where("relation","1")
							->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errormsg' => '您不是业主无权访问']);
		}else
		{
			return json(["errCode"=>"0",'msg' => '成功']);
		}	
	}

	//获取该业主下面绑定的所有用户
	public function visitorlist()
	{
		$user_id=request()->post("user_id");
		$userlist=Db::table("personnel")
					->where("parent_user_id",$user_id)
					->order("id desc")
					->select();
		foreach($userlist as $key=>$value)
		{
			if($value["relation"]=="2")
			{
				$userlist[$key]["relation"]="家人";
			}
			if($value["relation"]=="3")
			{
				$userlist[$key]["relation"]="租客";
			}
		}
		return json(["errCode"=>"0",'userlist' => $userlist]);
	}

	//业主
	public function ewm()
	{
		
		$user_id=request()->post();
	}

	//家人访客扫码绑定
	public function bind()
	{
		$user_id=request()->get("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1",'errMsg' => "请用小程序扫码绑定"]);
		}
		$id=request()->get("personnel_id");
		$userinfo=Db::table("personnel")->where("id",$id)->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => "用户信息不存在，无法绑定"]);
		}
		//if(strlen())
		$parent_user_id=request()->get("parent_user_id");
		if($parent_user_id！==$userinfo["parent_user_id"])
		{
			return json(["errCode"=>"1",'errMsg' => "用户信息错误，请重新扫码绑定"]);
		}
		$res=Db::table("personnel")->where("id",$id)->update(["user_id"=>$user_id]);
		if($res)
		{
			return json(["errCode"=>"1",'errMsg' => "用户绑定失败，请重新扫码绑定"]);
		}else
		{
			return json(["errCode"=>"0",'errMsg' => "用户绑定失败，请重新扫码绑定"]);
		}
	}

	//查询是否可以人脸上传
	public function verifyFace()
	{
		$user_id=request()->post("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1",'errMsg' => "您还没有绑定房号"]);
		}
		$userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => "您还没有绑定房号"]);
		}else{
			return json(["errCode"=>"0",'Msg' => "操作成功"]);
		}
	}

	public function contract()
	{
		$user_id=request()->post("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1",'errMsg' => "您还没有绑定房号"]);
		}
		$userinfo=Db::table("personnel")->where("user_id",$user_id)->find();
		if(empty($userinfo))
		{
			return json(["errCode"=>"1",'errMsg' => "您还没有绑定房号"]);
		}else{
			//根据小区id查询小区信息
			$communityinfo=Db::table("community")->where("id",$userinfo["community_id"])->find();
			if(empty($communityinfo)){
				return json(["errCode"=>"1",'errMsg' => "未查到小区信息"]);
			}
			return json(["errCode"=>"0",'Msg' => "操作成功","phone"=>$communityinfo["phone"]]);
		}
	}
}